from flask import Flask, render_template, url_for, session, redirect, request
from . import index
from app.models import Article, User, Item


@index.route('/', methods=("GET", "POST"))
def index():
    print('index函数开始')
    users = {}
    comm = Item.query.all()
    if request.method == "GET":
        arts = Article.query.order_by(Article.arc_click).all()
        for item in arts:
            users[item] = User.query.filter_by(user_name=item.arc_author).first()
    if 'username' in session:
        if session['username']==None:
            mylogin = '登录'
            login = '/login'
            publish = '/publish'
            return render_template('index.html', users=users, mylogin=mylogin, login=login, publish=publish, item=comm)
        else:
            mylogin = session['username']
            login = '/usercenter'
            publish = '/publish'
            return render_template('index.html', users=users, login=login, mylogin=mylogin, publish=publish, item=comm)
    else:
        mylogin = '登录'
        login = '/login'
        publish = '/publish'
        return render_template('index.html', users=users, mylogin=mylogin, login=login, publish=publish, item=comm)



